package com.augurit.gzcgwsp.commonex.util.lang;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import sun.misc.BASE64Decoder;

import com.augurit.common.util.lang.DateUtils;

public class DateUtilsEX extends DateUtils{
	/**
	 * 将日期转换为中文形式
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static String convertDate2Chinese(Date date) throws ParseException{	
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		int year = ca.get(Calendar.YEAR);
		int month = ca.get(Calendar.MONTH) + 1;
		int day = ca.get(Calendar.DAY_OF_MONTH);
		
		String strYear = convertNumber2Chinese(year);
		String strMonth = "";
		if(month < 10){
			strMonth = convertNumber2Chinese(month);
		}else if(month == 10){
			strMonth = "十";
		}else{
			strMonth = "十"+convertNumber2Chinese(month%10);
		}
		String strDay = "";
		if(day < 10){
			strDay = convertNumber2Chinese(day);
		}else if(day == 10){
			strDay = "十";
		}else if(day == 20){
			strDay = "二十";
		}else if(day == 30){
			strDay = "三十";
		}else{
			
			if("一".equals(convertNumber2Chinese(day/10))){
				strDay = "十" + convertNumber2Chinese(day%10);
			}else{
				strDay = convertNumber2Chinese(day/10) + "十" + convertNumber2Chinese(day%10);
			}
		}
		return strYear+"年"+strMonth+"月"+strDay+"日";
	}
	/**
	 * 大写日期还原
	 * @param str
	 * @return
	 */
	public static String convertNumByStr(String str){
		 if("十".equals(str)){
		        return "10";
		    }else if("十一".equals(str)){
		        return "11";
		    } else if("十二".equals(str)){
		        return "12";
	        }  else if("二十".equals(str)){
	            return "20";
	        } else if("三十".equals(str)){
	            return "30";
	        }else{
	        	
	        	return str.replace('〇','0').replace('○', '0').replace('一','1').replace(
	            '二','2').replace('三','3').replace('四','4').replace('五','5')
	            .replace('六','6').replace('七','7').replace('八','8').replace(
	                     '九','9');
	        }
	}
	
	
	/**
	 * 大写日期还原
	 * @param str
	 * @return
	 */
	public static String convertNumByDayStr(String str){
		 if("十".equals(str)){
		        return "10";
		    }else if("十一".equals(str)){
		        return "11";
		    } else if("十二".equals(str)){
		        return "12";
	        }  else if("二十".equals(str)){
	            return "20";
	        } else if("三十".equals(str)){
	            return "30";
	        }else{
	        	if(str.length()>1){
	        			if(str.length()==2){
	    	        		if("十".equals(str.substring(0, 1))){
	    	        			String day=convertNumByStr(str.substring(1, 2));
	    	        			String value=String.valueOf(10+Integer.valueOf(day));
	    	        			return value;
	    	        		}
	    	        	}else if(str.length()==3){
	    	        		if("二十".equals(str.substring(0, 2))){
	    	        			String day=convertNumByStr(str.substring(2, 3));
	    	        			String value=String.valueOf(20+Integer.valueOf(day));
	    	        			return value;
	    	        		}
	    	        		if("三十".equals(str.substring(0, 2))){
	    	        			String day=convertNumByStr(str.substring(2, 3));
	    	        			String value=String.valueOf(30+Integer.valueOf(day));
	    	        			return value;
	    	        		}
	    	        	}
	        		
	        	}
	        	return str.replace('〇','0').replace('○', '0').replace('一','1').replace(
	            '二','2').replace('三','3').replace('四','4').replace('五','5')
	            .replace('六','6').replace('七','7').replace('八','8').replace(
	                     '九','9');
	        }
	}
	
	/**
	 * 将日期转换为中文普通格式
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static String convertCommonDate(Date date) throws ParseException{
		if(date!=null&&!"".equals(date)){
			Calendar ca = Calendar.getInstance();
			ca.setTime(date);
			int year = ca.get(Calendar.YEAR);
			int month = ca.get(Calendar.MONTH) + 1;
			int day = ca.get(Calendar.DAY_OF_MONTH);
			return year+"年"+month+"月"+day+"日";
		}
		return "";	
	}
	
	public static String dateFormate(java.util.Date date,String formate){
		if(date==null){
			return null;
		}else{
			SimpleDateFormat format = new SimpleDateFormat(formate);
			String str = format.format(date); 
			return str;
		}
	}
	   public static Date dateFormate(String value,String formate) throws ParseException{
	        if(value==null){
	            return null;
	        }else{
	            SimpleDateFormat format = new SimpleDateFormat(formate);
	            Date date = format.parse(value); 
	            return date;
	        }
	    }
	/**
	 * 将数字转换为中文 主要是年月的转换
	 * @param num
	 * @return
	 */
	public static String convertNumber2Chinese(int num){
	    String result="";
	    String numStr=String.valueOf(num);
	     if("10".equals(numStr)){
	        return "十";
	    }else if("11".equals(numStr)){
	        return "十一";
	    } else if("12".equals(numStr)){
	        return "十二";
        }  else if("20".equals(numStr)){
            return "二十";
        } else if("30".equals(numStr)){
            return "三十";
        }else{
            return String.valueOf(num).replace('0', '〇').replace("1", "一").replace("2",
            "二").replace("3", "三").replace("4", "四").replace("5", "五")
            .replace("6", "六").replace("7", "七").replace("8", "八").replace(
                    "9", "九");
        }
	}
	
	/**
	 * 获取当前年份
	 * @return
	 */
	public static String getCurYear(){
	    Date da = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(da);
	    int year=cal.get(Calendar.YEAR);
	    return String.valueOf(year);
	}
	/**
	 * 获取当前月
	 * @return
	 */
	public static String getCurMonth(){
	    Date da = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(da);
        int month = cal.get(Calendar.MONTH) + 1;
        if(month<10)
        	return "0"+month;
	    return String.valueOf(month);
	}
	
	/**
	 * 获取当前日
	 * @return
	 */
	public static String getCurDay(){
	    Date da = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(da);
    	int day = cal.get(Calendar.DAY_OF_MONTH);
    	if(day<10){
    		 return "0"+day;
    	}
	    return String.valueOf(day);
	}
	
	public static void main(String[] args) {
		
    }
	/**
     * 将数字转换为中文
     * @param num
     * @return
     */
	public static String intToCnNum(int num) {
	    StringBuilder sb = new StringBuilder();
	    final String CN_NUM = "〇一二三四五六七八九";
	    final String CN_W = "十百千";
	    int n = num;
	    for (int i = 0; n > 0; i++) {
	     int g = n % 10;
	     if ((i >= 1) && (i <= 3)) {
	      if (g != 0) {
	       sb.insert(0, CN_W.charAt(i - 1));
	      }
	     } else if (i == 4) {
	      sb.insert(0, '万');
	     } else if ((i >= 5) && (i <= 7)) {
	      if (g != 0) {
	       sb.insert(0, CN_W.charAt(i - 4 - 1));
	      }
	     } else if (i == 8) {
	      sb.insert(0, '亿');
	     } else if ((i >= 9) && (i <= 10)) {
	      if (g != 0) {
	       sb.insert(0, CN_W.charAt(i - 8 - 1));
	      }
	     }
	     if ((g != 0) || ((i > 0) && (sb.charAt(0) != '〇'))) {
	      sb.insert(0, CN_NUM.charAt(g));
	     }
	     n = n / 10;
	    }
	    if ((sb.length() > 1) && (sb.charAt(sb.length() - 1) == '〇')) {
	     sb.setLength(sb.length() - 1);
	    }
	    String result=sb.toString();
	    //去掉一十
	    if(result.equals("一十")){
	        result=result.substring(1,result.length());
	    }
	    
	    return result;
	 }

	
	
	/**
	 * 将日转换成中文格式的日
	 * @param day
	 * @return
	 */
	public static String convertNumber2ChineseByDay(int day){
	    String strDay = "";
        if(day < 10){
            strDay = convertNumber2Chinese(day);
        }else if(day == 10){
            strDay = "十";
        }else if(day == 20){
            strDay = "二十";
        }else if(day == 30){
            strDay = "三十";
        }else{
            if(day/10 == 1){
                strDay = "十" + convertNumber2Chinese(day%10);
            }else{
                strDay = convertNumber2Chinese(day/10) + "十" + convertNumber2Chinese(day%10);
            }
        }
        return strDay;
	}
	
	/**
	 * 年转大写
	 * @param year
	 * @return
	 */
	public static String convertYearToChar(int year) {
	    char[] c = Integer.toString(year).toCharArray();
	    StringBuffer sb = new StringBuffer();
	    Map<Integer, String> map = new HashMap<Integer, String>();
	    String[] capital = { "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
	    int i = 0;
	    for (String str : capital) {
	        map.put(i++, str);
	    }
	    for (char charYear : c) {
	      sb.append(map.get(Integer.parseInt(Character.toString(charYear))));
	      }
	      return sb.toString();
	    }

	    /**
	    * 月份转换成中文
	    *  
	    * @param month
	    * 1 ~ 12 之间
	    * @return 中文月份
	    */
	    public static String convertMonthToChar(int month) {

	    Map<Integer, String> hashtable = new HashMap<Integer, String>();
	    String[] capital = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十","十一", "十二" };
	    int i = 0;
	    for (String str : capital) {
	        hashtable.put(++i, str);
	    }
	        String str = hashtable.get(month);
	        return str;
	    }

	    /**
	    * 日期转换成中文
	    *  
	    * @param day
	    * 1 ~ 31 之间
	    * @return 中文日期
	    */
	    public static String convertDayToChar(int day) {
	    StringBuffer sb = new StringBuffer();
	    Map<Integer, String> map = new HashMap<Integer, String>();
	    String[] capital = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
	    int i = 0;
	    for (String str : capital) {
	        map.put(++i, str);
	    }
	    char[] c = Integer.toString(day).toCharArray();
	    if (2 == c.length) {
    	    if (c[0] == '1') {
    	        sb.append(capital[9]);
        	    if (c[1] != '0') {
        	    sb.append(map.get(Integer.parseInt(Character.toString(c[1]))));
        	    }
    	    }
    	    if (c[0] == '2') {
    	        sb.append(capital[1] + capital[9]);
        	    if (c[1] != '0') {
            	    sb.append(map.get(Integer.parseInt(Character.toString(c[1]))));
            	 }
    	    }
    	    if (c[0] == '3') {
    	        sb.append(capital[2] + capital[9]);
        	    if (c[1] != '0') {
        	        sb.append(map.get(Integer.parseInt(Character.toString(c[1]))));
        	    }
    	    }
	    }else {
	       sb.append(map.get(Integer.parseInt(Character.toString(c[0]))));
    	}
	    return sb.toString();
	   }
	    
	    /**
	     * 两个日期的相隔的天数
	     * @param beginDate
	     * @param endDate
	     * @return
	     * @throws ParseException
	     */
	    public static int getDaysBetween (Date beginDate, Date endDate) throws ParseException
	    {
	          int days=0;
	    	  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	          Date bDate = beginDate;
	          Date eDate = endDate;
	          Calendar d1 = new GregorianCalendar();
	          d1.setTime(bDate);
	          Calendar d2 = new GregorianCalendar();
	          d2.setTime(eDate);
	          days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
	         int y2 = d2.get(Calendar.YEAR);
	         if (d1.get(Calendar.YEAR) != y2)
	         {
	                d1 = (Calendar) d1.clone();
	               do{
	                    days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);//得到当年的实际天数
	                    d1.add(Calendar.YEAR, 1);
	                 }while (d1.get(Calendar.YEAR) != y2);
	         }
	         return days;
	    }
	    
	    /**
	     * 根据当前时间获取几天前的日期
	     * @param nowdate 当前时间
	     * @param day 天数
	     * @param format 生成的日期格式
	     * @return
	     */
	    public static String getDateByDay(Date nowdate,int day,String format){
	    	    Calendar now =Calendar.getInstance();   
	    	    now.setTime(nowdate);   
	    	    now.set(Calendar.DATE,now.get(Calendar.DATE)+day);   
	    	    SimpleDateFormat sdf = new SimpleDateFormat(format);
	    	    return sdf.format(now.getTime());
	    }
	    
	    /**
	     * yyyy年MM月dd日格式的日期String转换成date类型
	     * @param dateStr
	     * @return
	     */
	    public static Date getStringToDate(String dateStr){
	    	SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
			   Date date = null;
			try {
				date = formatter.parse(dateStr);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return date;
	    }
	    
	    /**
	     * yyyy年MM月dd日格式的日期String转换成date类型
	     * @param dateStr
	     * @return
	     */
	    public static Date getStringToDate(String dateStr,String formate){
	    	SimpleDateFormat formatter = new SimpleDateFormat(formate);
			   Date date = null;
			try {
				date = formatter.parse(dateStr);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return date;
	    }
	    
	    /**
	     * 获取两位数的年和两位数的月和两位数的日  如2012年3月9日，变成120309
	     * @return
	     */
	    public static String getYearAndMonthAndDay(){
	    	Date date = new Date();
			SimpleDateFormat myFmt=new SimpleDateFormat("yyMMdd");		
			String rq = myFmt.format(date);//日期 6位
	       	return rq;
	    }
	    
	    /**
	     * timestamp转Date
	     * @param timestamp
	     * @return
	     */
	    public static Date timestamp2Date(Timestamp timestamp){
	    	return new Date(timestamp.getTime());
	    }
	    
	    /**
	     * Date转timestamp
	     * @param timestamp
	     * @return
	     */
	    public static Timestamp date2Timestamp(Date date){
	    	SimpleDateFormat myFmt=new SimpleDateFormat("yyyy-MM-dd HH:mm:sss");		
			String rqStr = myFmt.format(date);//
			Timestamp ts = Timestamp.valueOf(rqStr);
			return ts;
	    }
	    
	    
	    /**
	     *根据当前时间获取几天前（后）的日期
	     * @param date
	     * @return
	     */
	    public static Date getDateByDay(Date date,int days){
	    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	    	Calendar calendar = Calendar.getInstance();      
	    	calendar.setTime(date);   
	    	calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)+days);   
	    	return calendar.getTime();   
	    }
	    
	    /**
	     * 获取几个月后的时间,
	     * @param date
	     * @param isLastDay 是否为最后一天
	     * @param month
	     * @return
	     */
	    public static Date getDateByMonth(Date date,int month,boolean isLastDay){
	    	Calendar calendar = Calendar.getInstance();      
	    	calendar.setTime(date);   
	    	calendar.add(Calendar.MONTH, month);
	    	if(isLastDay){
	    		int maxDay=calendar.getActualMaximum(Calendar.DAY_OF_MONTH); 
		    	calendar.set(Calendar.DAY_OF_MONTH, maxDay);
	    	}
	    	
	    	return calendar.getTime();   
	    }
	    /**
	     * 中文年转换成数字
	     * @param dateStr
	     * @return
	     */
	    public static String ChineseYear2Number(String dateStr){
			String str = "";
			if(StringUtils.isNotBlank(dateStr)){
				str = dateStr.replace('〇', '0').replace('○', '0').replace("一", "1").replace("二",
	            "2").replace("三", "3").replace("四", "4").replace("五", "5")
	            .replace("六", "6").replace("七", "7").replace("八", "8").replace(
	                    "九", "9");
			}
			return str;
		}
		/**
		 * 中文月转换成数字
		 * @param dateStr
		 * @return
		 */
		public static String ChineseMonth2Number(String dateStr){
			String str = "";
			if(StringUtils.isNotBlank(dateStr)){
				if(dateStr.contains("十")){
					if(dateStr.equals("十")){
						str = dateStr.replace("十", "10");
					}else{
						str = dateStr.replace("十", "").replace("一", "11").replace("二","12");
					}
					
				}else{
					str = dateStr.replace("一", "1").replace("二",
		            "2").replace("三", "3").replace("四", "4").replace("五", "5")
		            .replace("六", "6").replace("七", "7").replace("八", "8").replace(
		                    "九", "9");
				}
				
			}
			return str;
		}
		
		/**
		 * 中文日转换成数字
		 * @param dateStr
		 * @return
		 */
		public static String ChineseDay2Number(String dateStr){
			String str = "";
			if(StringUtils.isNotBlank(dateStr)){
				if(dateStr.contains("十")){//含有10的
					if(dateStr.equals("十")){//10
						str = dateStr.replace("十", "10");
					}else if(dateStr.equals("二十")){//20
						str = dateStr.replace("二十", "20");
					}else if(dateStr.equals("三十")){//30
						str = dateStr.replace("三十", "30");
					}else if(dateStr.startsWith("十")){//十开头的
						str = dateStr.replace("十", "").replace("一", "11").replace("二",
			            "12").replace("三", "13").replace("四", "14").replace("五", "15")
			            .replace("六", "16").replace("七", "17").replace("八", "18").replace(
			                    "九", "19");
					}else{//二十一、三十一这种
						str = dateStr.replace("十", "").replace("一", "1").replace("二",
			            "2").replace("三", "3").replace("四", "4").replace("五", "5")
			            .replace("六", "6").replace("七", "7").replace("八", "8").replace(
			                    "九", "9");
					}
					
				}else{//十以下的
					str = dateStr.replace("一", "1").replace("二",
		            "2").replace("三", "3").replace("四", "4").replace("五", "5")
		            .replace("六", "6").replace("七", "7").replace("八", "8").replace(
		                    "九", "9");
				}
				
			}
			return str;
		}
}
